// 1. 引入Vue和Router
import Vue from 'vue'
import Router from 'vue-router'

// 2. 全局注册路由插件
// 因为这里全局注册了, 我们才能在每一个.vue文件中去使用$router和$route
Vue.use(Router)

// 3. 静态引入(引入项目的主体架子)
import Layout from '@/layout'

// 4. 引入模块
// 引入多个模块的规则
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'

/**
 * 动态路由数组
 * 动态路由(需要权限才可以访问的) 我们这里准备一个数组存放
 */
export const asyncRoutes = [
  departmentsRouter,
  settingRouter,
  employeesRouter,
  permissionRouter,
  approvalsRouter,
  attendancesRouter,
  salarysRouter,
  socialRouter
]

/**
 * constantRoutes
 * 静态路由规则数组
 * 后期在这个数组里面配置的所有的路由规则, 都不需要做权限控制
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      // 元信息: 携带一些需要传递给页面的额外信息
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },

  {
    path: '/import',
    component: Layout,
    children: [{
      path: '',
      name: 'import',
      component: () => import('@/views/import/index')
    }],
    hidden: true
  }

  // 404 必须放在规则数组的最后面
  // { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // 路由模式
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }), // 让页面回到顶部
  routes: [
    ...constantRoutes
    // ...asyncRoutes
  ]
})

const router = createRouter()

// 路由重置
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
